const state = {
    songList: [],
    nextId: '',
    curId: '',
    preId: '',
    
}

const actions = {

}
const mutations = {
    GET_SONGLIST(state, val) {
        state.songList = val || [];
    },


    // 获取歌曲下个ID
    NEXT_ID(state, songId) {
        if (state.nextId == '') {
            state.nextId = state.songList[0].id;
            return;
        }
        for (let i = 0; i < state.songList.length; i++) {
            if (state.songList[state.songList.length - 1].id == songId) {
                state.nextId = state.songList[0].id;
                break;
            } else if (state.songList[i].id == songId) {
                state.nextId = state.songList[i + 1].id;
                break;
            } else {
                if (state.songList.length != 0) {
                    state.nextId = state.songList[0].id;
                } else {
                    state.nextId = state.curId;
                }

            }


        }
    },
    // 获取歌曲上个ID
    PRE_ID(state, songId) {
        if (state.nextId == '') {
            state.preIdId = state.songList[0].id;
            return;
        }
        for (let i = 0; i < state.songList.length; i++) {
            if (state.songList[state.songList.length - 1].id == songId) {
                state.preId = state.songList[0].id;
                break;
            } else if (state.songList[i].id == songId) {

                if (i == 0) {
                    state.preId = state.songList[state.songList.length - 1].id;
                } else {
                    state.preId = state.songList[i - 1].id || state.songList[0].id;
                }
                break;
            } else {
                if (state.songList.length != 0) {
                    state.preId = state.songList[0].id;
                } else {
                    state.preId = state.curId;
                }

            }


        }
    },
    // 保存当前歌曲id
    CUR_ID(state, songId) {
        state.curId = songId;
        for (let i = 0; i < state.songList.length; i++) {
            if (songId == state.songList[i].id) {
                state.curMsg = state.songList[i]
            }
        }
    }
}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
}